package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.pactera.madp.common.core.constant.CommonConstants;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.sync.SyncDTO;
import com.pactera.madp.cp.api.em.beta.NewsChannelEnum;
import com.pactera.madp.cp.api.em.beta.NewsStateEnum;
import com.pactera.madp.cp.api.em.beta.ChoreographyEnum;
import com.pactera.madp.cp.api.entity.beta.WorkChoreographyEntity;
import com.pactera.madp.cp.api.entity.sync.SyncChoreographyEntity;
import com.pactera.madp.cp.api.vo.beta.BetaChoreographyVO;
import com.pactera.madp.cp.client.BetaApiClient;
import com.pactera.madp.cp.service.WorkChoreographyService;
import com.pactera.madp.cp.service.sync.SyncBetaChoreography;
import com.pactera.madp.cp.service.sync.SyncChoreographyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.pactera.madp.common.core.constant.CommonConstants.SUPER_ADMIN_USERID;
import static com.pactera.madp.common.core.constant.CommonConstants.SUPER_ADMIN_USERNAME;

/**
 * @title 同步精编资讯-接口实现
 *   - ① 发起网络请求获取资讯数据
 *   - ② 根据类型同步精编资讯到同步表
 *   - ③ 根据类型同步精编资讯到业务表
 * @date 2022-06-11
 * @author Deming.Chang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SyncBetaChoreographyImpl implements SyncBetaChoreography {

    /**
     * 贝塔资讯API客户端
     */
    private final BetaApiClient client;
    /**
     * 精编资讯同步表-业务服务
     */
    private final SyncChoreographyService syncChoreographyService;
    /**
     * 精编资讯业务表-业务服务
     */
    private final WorkChoreographyService workChoreographyService;

    @Override
    public String[] handleSyncParams(SyncDTO dto) {
        Map<String, Object> params = dto.getParams();
        return StrUtil.split(MapUtil.getStr(params, PARAMS_KEY_TYPES), StrUtil.COMMA);
    }

    @Override
    public Boolean syncChoreography(String[] types) {
        for (String type : types) {
            ChoreographyEnum enums = ChoreographyEnum.findByType(Integer.parseInt(type));
            // 1.0 发起网络请求获取资讯数据
            log.info("[Madp] 开始同步[精编-{}]数据", enums.getName());
            R<BetaChoreographyVO> result = client.getChoreographyInformationList(enums);
            BetaChoreographyVO vo = result.getData();
            if(CommonConstants.FAIL == result.getCode() || ObjectUtil.isNull(vo)){
                log.warn("[Madp] 结束同步[精编-{}]数据,同步状态：[false],网络异常或无数据", enums.getName());
                continue;
            }
            // 2.0 将数据插入或更新到同步表
            SyncChoreographyEntity entity = new SyncChoreographyEntity();
            entity.setNewsKey(vo.getInfo().getKey())
                    .setType(enums.getType())
                    .setInfo(JSONUtil.toJsonStr(vo.getInfo()))
                    .setModuleAndNews(JSONUtil.toJsonStr(vo.getModuleAndNews()));
            syncChoreographyService.saveOrUpdate(entity);
            // 4.0 根据类型同步精编资讯业务表(首先验证是否同步，如果已经同步则不同步)
            if(ObjectUtil.isNull(workChoreographyService.getById(entity.getNewsKey()))){
                WorkChoreographyEntity workEntity = client.coverChoreographyInformation(entity);
                workEntity.setState(NewsStateEnum.UN_PUBLISH.getState())
                        .setChannel(NewsChannelEnum.BETA.getChannel())
                        .setCreateUserId(SUPER_ADMIN_USERID)
                        .setCreateUserName(SUPER_ADMIN_USERNAME);
                Boolean state = workChoreographyService.save(workEntity);
                log.info("--->[Madp] 结束同步[精编-{}]数据,同步状态：[{}]", enums.getName(), state);
            }else {
                log.warn("--->[Madp] 结束同步[精编-{}]数据,同步状态：[false], 无需同步", enums.getName());
            }
        }
        return Boolean.TRUE;
    }

}
